﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace University.Workshop.DataAccess
{
    public static class HospitalizationDataAccess
    {
        static MedicalCenterEntities entities = MedicalCenterEntities.Instance;

        public static ICollection<Model.Bed> ObtainAllBeds() 
        {
            var beds = from bed in entities.Beds
                       select bed;

            return (List<Model.Bed>)beds.ToList();
        }

        public static bool RegisterNewHospitalization(Model.Hospitalization hospitalization)
        {
            try
            {
                if(hospitalization!=null)
                {
                    entities.Hospitalizations.Add(hospitalization);
                    entities.SaveChanges();

                    Model.Bed bed= entities.Beds.Find(hospitalization.BedId);
                    bed.State = "Ocupado";
                    entities.SaveChanges();
                }
            }
            catch (NullReferenceException)
            {
                return false;
            }
            return true;
        }

        public static ICollection<Model.Hospitalization> ObtainActiveHospitalizations()
        {
            var query = from item in entities.Hospitalizations
                        where item.State == "Activo"
                       select item;

            return (List<Model.Hospitalization>)query.ToList();
        }

        public static Model.Hospitalization ObtainHospitalization(int hospitalizationId)
        {
            return entities.Hospitalizations.Find(hospitalizationId);
        }

        public static ICollection<Model.HospitalizationMedicine> ObtainMedicinesFromHospitalizationId(int hospitalizationId)
        {
            var query = from item in entities.HospitalizationMedicines
                        where item.HospitalizationId == hospitalizationId
                        select item;

            return (List<Model.HospitalizationMedicine>)query.ToList();
        }

        public static ICollection<Model.HospitalizationMedicalSupply> GetMedicalSuppliesFromHospitalizationId(int hospitalizationId)
        {
            var query = from item in entities.HospitalizationMedicalSupplies
                        where item.HospitalizationId == hospitalizationId
                        select item;

            return (List<Model.HospitalizationMedicalSupply>)query.ToList();
        }

        public static ICollection<Model.HospitalizationClinicalReport> GetClinicalReportsFromHospitalizationId(int hospitalizationId)
        {
            var query = from item in entities.HospitalizationClinicalReports
                        where item.HospitalizationId == hospitalizationId
                        select item;

            return (List<Model.HospitalizationClinicalReport>)query.ToList();
        }

        public static void ModifyHospitalizationData(List<Model.HospitalizationMedicalSupply> medicalSuppliesHospitalization)
        {
            try
            {
                var deleteMedicines = from medicine in entities.HospitalizationMedicines
                                      where medicine.HospitalizationId == medicalSuppliesHospitalization[0].HospitalizationId
                                      select medicine;

                foreach (var medicine in deleteMedicines)
                {
                    entities.HospitalizationMedicines.Remove(medicine);
                }

                entities.SaveChanges();
            }catch(Exception){}
        }

        public static void AddClinicalReport(Model.HospitalizationClinicalReport clinical)
        {
            entities.HospitalizationClinicalReports.Add(clinical);
            entities.SaveChanges();
        }
    }
}
